﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PuzzleNew
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        private ImageManager PuzzleNew;
        
        private Image img;

        private void GameArea_Load(object sender, EventArgs e)
        {

        }
        private void FrmMain_Load(object sender, EventArgs e)
        {
           

        }
        private void pictureBoxLeft_Click(object sender, EventArgs e)
        {

        }

        private void pictureBoxRight_Click(object sender, EventArgs e)
        {

        }
        private void btnAdd_Click_1(object sender, EventArgs e)
        {
            //添加图片
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();
            //防止是空白
            if (ofd.FileName!=null && ofd.FileName!="")
            {
                Game.img = Image.FromFile(ofd.FileName).GetThumbnailImage(700, 700, new Image.GetThumbnailImageAbort(delegate { return false; }), IntPtr.Zero);
            }
        }
        private void btnStartGame_Click_1(object sender, EventArgs e)
        {
            //防止空白并提示
            if (Game.img==null)
            {
                MessageBox.Show("Please Insert An Image First!");
                return;
            }
            img = Game.img;
            pictureBoxRight.Image = img.GetThumbnailImage(300, 300, new Image.GetThumbnailImageAbort(delegate { return false; }), IntPtr.Zero);
            PuzzleNew = new ImageManager(img, this.pictureBoxLeft.Size);
            pictureBoxLeft.Image = PuzzleNew.Show(this.pictureBoxLeft.Size);
        }

        
        private void pictureBoxLeft_MouseClick(object sender, MouseEventArgs e)
        {
            //防止空白并提示
            if (Game.img == null)
            {
                MessageBox.Show("Please Insert An Image First!");
                return;
            }
            if (PuzzleNew.Rotate(e.X / (PuzzleNew.Width / PuzzleNew.N), e.Y / (PuzzleNew.Width / PuzzleNew.N)))
            {
                //重绘
                pictureBoxLeft.Image = PuzzleNew.Show(this.pictureBoxLeft.Size);
                //判断之后确定是否继续
                if (PuzzleNew.Judge())
                {
                    if (MessageBox.Show("恭喜过关", "是否重新玩一把", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                       
                        PuzzleNew.PuzzleTurn();
                        pictureBoxLeft.Image = PuzzleNew.Show(this.pictureBoxLeft.Size);

                    }
                    else
                    {
                        this.Close();
                    }
                }

            }
        }
      

        private void btnAboutMe_Click(object sender, EventArgs e)
        {
            FrmAboutMe frm = new FrmAboutMe();
            frm.ShowDialog();
        }
        
    }
}
